﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class CreateUser : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnCreate_Click(object sender, EventArgs e)
    {
        string userName = txtUserId.Text;
        
        //this value is either encrypted or hashed and is never displayed 
        string password = txtPassword.Text;
        string email = txtEmail.Text;
        string passwordQuestion = txtSecretQuestions.Text;

        //this value is either encrypted or hashed and is never displayed 
        string passwordAnswer = txtPasswordAnswer.Text;

        MembershipCreateStatus results;
        Membership.CreateUser(userName, password, email, passwordQuestion, passwordAnswer, true, out results);

        lblResults.Visible = true;

        switch (results)
        {
            case MembershipCreateStatus.Success:
                txtUserId.Text = null;
                txtPassword.Text = null;
                txtEmail.Text = null;
                txtSecretQuestions.Text = null;
                txtPasswordAnswer.Text = null;
                Response.Redirect("Login.aspx");
                lblResults.Text = "User successfully created!";
                break;
            case MembershipCreateStatus.InvalidUserName:
                lblResults.Text = "The username format was invalid.  Please enter a different username.";
                break;
            case MembershipCreateStatus.InvalidPassword:
                lblResults.Text = "The password was invalid:  A password cannot be an empty string and must also meet the pasword strength requirements of the configured provider.  Please enter a new password.";
                break;
            case MembershipCreateStatus.InvalidEmail:
                lblResults.Text = "The email format was invalid.  Please enter a different username.";
                break;
            case MembershipCreateStatus.InvalidQuestion:
                lblResults.Text = "The password question format was invalid.  Please enter a different question.";
                break;
            case MembershipCreateStatus.InvalidAnswer:
                lblResults.Text = "The password answer format was invalid.  Please enter a different answer.";
                break;
            case MembershipCreateStatus.DuplicateUserName:
                lblResults.Text = "The username is already in use.  Please enter a new username.";
                break;
            case MembershipCreateStatus.DuplicateEmail:
                lblResults.Text = "The email address is already in use.  Please enter a different email address.";
                break;
            default:
                lblResults.Text = "An error occurred while creating the user.";
                break;
        }


    }
}
